import { defineStore } from 'pinia';
import type { User } from '../types/auth';
import { setStorage, getStorage, USER_INFO_KEY } from '../utils/storage';

// 定义用户信息类型（与../types/auth.ts对应）
interface AuthState {
  user: User | null; // 当前登录用户信息
}

// 创建权限相关的Store
export const useAuthStore = defineStore('auth', {
  state: (): AuthState => ({
    user: getStorage(USER_INFO_KEY)
  }),
  actions: {
    // 设置用户信息
    setUser(user: User | null) {
      this.user = user;
      setStorage(USER_INFO_KEY, user);
    },
    // 清除用户信息（退出登录时用）
    clearUser() {
      this.user = null;
      setStorage(USER_INFO_KEY, null);
    }
  },
  getters: {
    // 判断是否已登录
    isLoggedIn: (state) => !!state.user
  }
});